package com.jl.commons.utils;

import com.alibaba.fastjson.JSONArray;
import com.jl.commons.constant.Constants;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 一些简单的杂项工具类
 * 针对某些单独的功能
 */
public class CommonUtils {

    /**
     * @param oldList 修改之前的list
     * @param newList 修改之后的list
     * @return
     */
    public static Map<String, List<Long>> GetListDifferent(List<Long> oldList, List<Long> newList) {
        Map<String, List<Long>> map = new HashMap<>();
        //如果都为null,则表示没有新增的也没有减少的，所有返回的都是空list
        if (null == oldList && null == newList) {
            map.put(Constants.LIST_DIFFERENT_ADD, new ArrayList<Long>());
            map.put(Constants.LIST_DIFFERENT_REDUCE, new ArrayList<Long>());
            return map;
        }
        //如果旧的list为null，新的list不为null，则表示全部都是新增的，则减少的为空的
        if (oldList == null && newList != null) {
            map.put(Constants.LIST_DIFFERENT_ADD, newList);
            map.put(Constants.LIST_DIFFERENT_REDUCE, new ArrayList<Long>());
            return map;
        }

        //如果新的list为null，旧的list不为null，则表示全部都删除掉了，即减少了
        if (newList == null && oldList != null) {
            map.put(Constants.LIST_DIFFERENT_ADD, new ArrayList<Long>());
            map.put(Constants.LIST_DIFFERENT_REDUCE, oldList);
            return map;
        }
        List<Long> newListTemp = new ArrayList<>();
        newListTemp = (List<Long>) ((ArrayList<Long>) newList).clone();
        //新增加的list
        newList.removeAll(oldList);
        //减少的list
        oldList.removeAll(newListTemp);
        map.put(Constants.LIST_DIFFERENT_ADD, newList);
        map.put(Constants.LIST_DIFFERENT_REDUCE, oldList);
        return map;
    }

    public static List<String> removeDuplicate(List<String> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        return list;
    }


    public static Map<Long, Integer> getStringListIdCount(List<String> stringList) {
        List<Long> longList = new ArrayList<>();
        for (String str : stringList) {
            List<Long> temp = new ArrayList<>();
            String[] strings = str.split(",");
            for (String s : strings) {
                if(NumberUtils.isNumber(s)){
                    temp.add(Long.parseLong(s));
                }
            }
            longList.addAll(temp);
        }
        return getIdCount(longList);
    }


    public static Map<Long, Integer> getIdCount(List<Long> ids) {
        Map<Long, Integer> map = new HashMap<>();
        for (Long tempId : ids) {
            if (map.containsKey(tempId)) {
                Integer count = map.get(tempId);
                map.put(tempId, count + 1);
            } else {
                map.put(tempId, 1);
            }
        }
        return map;
    }


    public static Map<Long,Integer> getFinishInspectionCount(List<Map<String,Long>> maps){
        Map<Long, Integer> returnMap = new HashMap<>();
        for(Map<String,Long> map :maps){
            returnMap.put(map.get("operatorId"),map.get("count").intValue());
        }
        return returnMap;
    }

    public static Map<Long,Integer> getConfirmInspectionCount(List<Map<String,Long>> maps){
        List<Long> ids = new ArrayList<>();
        for(Map<String,Long> map : maps){
            ids.add(map.get("monitorId"));
            ids.add(map.get("directorId"));
        }
        return getIdCount(ids);
    }

}
